package adke.gui.panels;

import adke.Util;
import adke.gui.animations.Animation;
import adke.listeners.PlayerListener;
import adke.managers.DropManager;
import adke.managers.PlayerManager;
import adke.models.Player;
import adke.models.powerups.Powerup;
import java.awt.AlphaComposite;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.VolatileImage;
import javax.swing.JPanel;

// deze is 2 genoemd omdat de eerste bij mij door corruptie ontoegankelijk was geworden

public class GFXPanel2 extends JPanel
{
    private static GFXPanel2 reference;
    
    // TODO: kijken naar VolatileImage
    
    // hier tekenen we op
    private BufferedImage drawer;

    private boolean clear;
    
    public GFXPanel2(int w, int h)
    {
        reference = this;
        
        clear = false;
        
        // beetje speling, zie GamePanel
        drawer = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        drawer = Util.optimizeImage(drawer);
        
        setDoubleBuffered(true);
        
        // doorzichtig
        setOpaque(false);
        
    }
    
    public static GFXPanel2 getInstance()
    {
        return reference;
    }
    
    // verkrijg de graphics voor de bufferedimage
    public Graphics getDrawer()
    {
        return drawer.getGraphics();
    }
    
    // clear buffer
    public void reset()
    {
        Graphics g = drawer.getGraphics();
        Graphics2D g2d = (Graphics2D)g;
        
        // maak scherm leeg
        Composite normalComposite = g2d.getComposite();
        Composite transparant = AlphaComposite.getInstance(AlphaComposite.CLEAR, 1);
        g2d.setComposite(transparant);
        g2d.fillRect(0, 0, drawer.getWidth(), drawer.getHeight());
        g2d.setComposite(normalComposite);
        
        // lui: maak gewoon nieuwe image
//        drawer = new BufferedImage(drawer.getWidth(), drawer.getHeight(), BufferedImage.TYPE_INT_ARGB);
//        drawer = Util.optimizeImage(drawer);
        
        repaint();
    }
    
    //tekent de buffer op het scherm
    public void paint(Graphics g)
    {
        g.drawImage(drawer, 0, 0, null);
    }
    
    public void paintRegion(Rectangle r)
    {
        Graphics g = getGraphics();
        g.drawImage(drawer, r.x, r.y, (int)r.getMaxX(), (int)r.getMaxY(), 
                r.x, r.y, (int)r.getMaxX(), (int)r.getMaxY(), null);
    }
}
